﻿using System;
using System.Globalization;
using System.Windows.Controls;

namespace Helpers.Validation
{
    public class ValidNumber : ValidationRule
    {
        
        private bool decimalPlaces = true;

        public bool DecimalPlaces
        {
            get { return decimalPlaces; }
            set { decimalPlaces = value; }
        }

        public ValidNumber()
        {
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            double number = 0;

            bool validNumber = Double.TryParse((string)value, out number);
            if (!validNumber) return new ValidationResult(false, "Invalid number.");

            if (!DecimalPlaces)
            {
                if (number % 1 != 0) return new ValidationResult(false, "Invalid number: the value can't have decimal places.");
            }

            return new ValidationResult(true, null);
        }

    }
}
